package com.hangzhou.xyj.manager;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.model.MessageModel;
import com.hangzhou.xyj.model.type.EPushMessageType;
import com.hangzhou.xyj.utils.StringUtil;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.Map;

/**
 * @author : neo.duan
 * @date : 	 2016/5/30
 * @desc : 有关消息通知管理器
 */
public class NotifyManager {
    public static int mSingleChatCount;//单聊消息的消息条数
    public static int mGroupChatCount;//群聊消息的消息条数
    public static int mAddGroupCount;//申请加入群的消息条数
    public static int mSystemCount;//系统消息条数
    // 消息通知ID
    private final String TAG = "NotifyManager";
    private String ACTION = "android.intent.action.DELETE_NOTIFICATION";//删除单聊通知
    private Map<Integer, Notification> mNotificationMap;//消息集合

    private NotifyManager() {
    }

    public static NotifyManager getInstance() {
        return NotifyManagerHolder.instance;
    }


    /**
     * 通知消息
     *
     * @param context
     * @param msg
     */
    public void notify(Context context, MessageModel msg, Intent intent) {
        if (context == null || msg == null)
            return;
//        if (XYJApplication.getUserInfo() == null) {
//            Log.d(TAG, "----没有登录，不进行推送-----");
//            return;
//        }
        if (XYJApplication.getUserInfo() != null
                && !CommonPreferences.getEnablePush(context, XYJApplication.getUserInfo().getUserId())) {//不允许推送
            return;
        }
        // notify显示文案
        int mNotifyId;
        String title = StringUtil.nullToEmpty(msg.getTitle());
        if (StringUtil.isBlank(title)) {
            title = context.getString(R.string.app_name);
        }
//        String uid = XYJApplication.getUserInfo().getUserId();
        String desc = "";
        String ticker = "";
        EPushMessageType msgType = EPushMessageType.valueOf(msg.getType());
        Intent deleteIntent = new Intent(ACTION);
        switch (msgType) {
            case SINGLE_CHAT://单聊
                ticker = "您收到" + (++mSingleChatCount) + "条消息";
                desc = ticker;
                mNotifyId = EPushMessageType.SINGLE_CHAT.getValue();
                break;
            case GROUP_CHAT://群聊
                ticker = "您收到" + (++mGroupChatCount) + "条群消息";
                desc = ticker;
                mNotifyId = EPushMessageType.GROUP_CHAT.getValue();
                break;
            case ADD_GROUP://申请
                ticker = "您收到" + (++mAddGroupCount) + "条申请加入圈子请求";
                desc = ticker;
                mNotifyId = EPushMessageType.ADD_GROUP.getValue();
                break;
            case SYSTEM:
            default://系统消息
                ticker = "您收到" + (++mSystemCount) + "条系统消息";
                desc = ticker;
                mNotifyId = EPushMessageType.SYSTEM.getValue();
                break;
        }
        deleteIntent.putExtra(DataKey.PUSH_TYPE, mNotifyId);
        //TODO 消息开关是否开发
//        boolean enabled = JMiPreferences.getEnablePush(context, uid);
//        if (!enabled)
//            return;
        Notification notification;
        NotificationManager manager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(DataKey.PUSH_TYPE, mNotifyId);
        //打开通知之后，发送广播
        PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setAutoCancel(false)//用户点击是否自动消失
                .setTicker(ticker)//设置提示的信息
                .setSmallIcon(R.drawable.ic_small_icon)//设置通知的图标
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon))//打开之后的图标
                .setContentTitle(title)//设置内容标题
                .setContentText(desc)//设置内容
                .setDeleteIntent(PendingIntent.getBroadcast(context, mNotifyId, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT))
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())//设置通知创建的时间
//                .setContentIntent(PendingIntent.getBroadcast(context, mNotifyId, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT))
                .setContentIntent(pIntent);//设置要启动的组件
//        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
//            notification = builder.getNotification();
//        } else {
        notification = builder.build();
//        }
//        mNotificationMap.put(mNotifyId, notification);
        manager.notify(mNotifyId, notification);
    }

    /**
     * 取消某类消息通知
     *
     * @param context
     */
    public void cancelMsg(Context context, EPushMessageType type) {
        NotificationManager manager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(type.getValue());
    }

    private static class NotifyManagerHolder {
        private static final NotifyManager instance = new NotifyManager();
    }


}
